<?php
$user_id = $GLOBALS['user_id'];

$user = new user($user_id); 
$user_info[$user_id] = $user->load_tables('user_settings,user_finances,user_stats');
$serfdom = new serfdom($user,$user_info,$user_id); 
$farm_item_info = $db->fetch_real_array("SELECT * FROM `farm_items`",'alias');

# Set which government bank account the money will go to
$account_id = 4;

//echo '<pre>';
//echo var_dump($user_info);
//echo '</pre>';

$currency = 'Quadrans';

$land = array();

$land['a']['short'] = 'very cold';
$land['b']['short'] = 'cold';
$land['c']['short'] = 'moderate/warm (as many wet spells as there is dry spells)';
$land['d']['short'] = 'hot';
$land['e']['short'] = 'very hot';

$land['a']['ov'] = 'in a Freezing climate';
$land['b']['ov'] = '(cold)';
$land['c']['ov'] = 'in a slightly warm environment';
$land['d']['ov'] = 'very warm climate';
$land['e']['ov'] = 'very hot area';

$land['1']['short'] = 'is very flat';
$land['2']['short'] = 'is flat';
$land['3']['short'] = 'has a bit of a mix (flatland and hills)';
$land['4']['short'] = 'is hilly';
$land['5']['short'] = 'is mountanious';

$land['1']['ov'] = 'Flat plains';
$land['2']['ov'] = 'Uneven fields';
$land['3']['ov'] = 'Uneven landscape with the old hill or two';
$land['4']['ov'] = 'Land in the hills';
$land['5']['ov'] = 'Mountanious area';


// the land_type is submitted as land_type on the form but on other pages its called v1
if (strlen(@$_REQUEST['land_type']) > 0){
	$land_options = str_split($_REQUEST['land_type']);
	$land_type = $_REQUEST['land_type'];
} elseif (@$_REQUEST['function'] == 'ov') {

} else {
	@$land_options = str_split($_REQUEST['v1']);
	@$land_type = $_REQUEST['v1'];
}

@$land_x = $land_options[0];
@$land_y = $land_options[1];
//var_dump($land_options);

	switch ($land_x)
	{
		case 'a';
			$rent_x = 2.17;
			$risk_of_fire = 0;
		break;
		case 'b';
			$rent_x = 4.17;
			$risk_of_fire = 20;
		break;
		case 'c';
			$rent_x = 6.17;
			$risk_of_fire = 60;
		break;
		case 'd';
			$rent_x = 8.17;
			$risk_of_fire = 80;
		break;
		case 'e';
			$rent_x = 10.17;
			$risk_of_fire = 100;
		break;
	}
	
	switch ($land_y)
	{
		case '1';
			$rent_y = 10.177;
		break;
		case '2';
			$rent_y = 8.177;
		break;
		case '3';
			$rent_y = 6.177;
		break;
		case '4';
			$rent_y = 4.177;
		break;
		case '5';
			$rent_y = 2.177;
			$risk_of_fire = $risk_of_fire - 4;
		break;
	}
@$price = $rent_x * $rent_y;
@$price = number_format($price,0);
//echo $price.' <- its there<br>';

switch (@$_REQUEST['function'])
{
    case 'rent_choose':
								echo 'Please choose the type of land to rent.';
								
								$form_data = '<table border="1" bordercolor="black" width="150">
									<tr>
									<td bgcolor=green align=center><input type="radio" name="land_type" value="e1"></td>
									<td bgcolor=green align=center><input type="radio" name="land_type" value="e2"></td>
									<td bgcolor=green align=center><input type="radio" name="land_type" value="e3"></td>
									<td bgcolor=black align=center>&nbsp;</td>
									<td bgcolor=black align=center>&nbsp;</td>
									<td align=right><small>warm</small></td>
									</tr>
									<tr>
									<td bgcolor=green align=center><input type="radio" name="land_type" value="d1"></td>
									<td bgcolor=green align=center><input type="radio" name="land_type" value="d2"></td>
									<td bgcolor=green align=center><input type="radio" name="land_type" value="d3"></td>
									<td bgcolor=brown align=center><input type="radio" name="land_type" value="d4"></td>
									<td bgcolor=black align=center>&nbsp;</td>
									<td align=right>&nbsp;</td>
									</tr>
									<tr>
									<td bgcolor=green align=center><input type="radio" name="land_type" value="c1"></td>
									<td bgcolor=green align=center><input type="radio" name="land_type" value="c2"></td>
									<td bgcolor=green align=center><input type="radio" name="land_type" value="c3"></td>
									<td bgcolor=brown align=center><input type="radio" name="land_type" value="c4"></td>
									<td bgcolor=brown align=center><input type="radio" name="land_type" value="c5"></td>
									<td align=right><small>(wet)</small></td>
									</tr>
									<tr>
									<td bgcolor=green align=center><input type="radio" name="land_type" value="b1"></td>
									<td bgcolor=green align=center><input type="radio" name="land_type" value="b2"></td>
									<td bgcolor=green align=center><input type="radio" name="land_type" value="b3"></td>
									<td bgcolor=brown align=center><input type="radio" name="land_type" value="b4"></td>
									<td bgcolor=brown align=center><input type="radio" name="land_type" value="b5"></td>
									<td align=right>&nbsp;</td>
									</tr>
									<tr>
									<td bgcolor=green align=center><input type="radio" name="land_type" value="a1"></td>
									<td bgcolor=green align=center><input type="radio" name="land_type" value="a2"></td>
									<td bgcolor=green align=center><input type="radio" name="land_type" value="a3"></td>
									<td bgcolor=brown align=center><input type="radio" name="land_type" value="a4"></td>
									<td bgcolor=brown align=center><input type="radio" name="land_type" value="a5"></td>
									<td align=right><small>cold</small></td>
									</tr>
									<tr>
									<td colspan=5 align=center><small>flat -> mountanoius</small></td>
									
									</tr>
									</table><input type="submit" name="submit" value="Proceed" />
									';
									
									echo html_form('/serfdom/rent_cost/', $form_data);



        break;
        case 'rent_cost':
								$short_x = $land[$land_x]['short'];
								$short_y = $land[$land_y]['short'];
									echo 'You choose to rent land that is '. $short_x .' and '. $short_y .'.';
									echo '<br>The government is requesting '. $price . ' '. $currency . ' per day for this land.';
										$options = array(
										 '/serfdom/rent_confirm/'.$_REQUEST['land_type'].'/' => 'gimmie gimmie gimmie',
										 '/serfdom/rent_choose/' => 'what? no way, lemme choose another area...',
										 '/serfdom/' => 'what? get yourself tae...',
										 );
									echo player_options($options);
					
					
					
        break;
        case 'rent_confirm':
								// need to ask the player how much money he wants to put into contract then work out how many days that will last
								// need to display a list of example days and costs
								$days = array();
								$days[1] = number_format($price * 1,0);
								$days[7] = number_format($price * 6.9,0);
								$days[10] = number_format($price * 9.7,0);
								$days[14] = number_format($price * 13.4,0);
								$days[30] = number_format($price * 29.3,0);
								$options = array();
								foreach ($days as $key => $value)
								{
								 //
									if ($key <> 1){ $plu = 'days'; } else { $plu = 'day'; }
									$options["/serfdom/rent_confirmed/$land_type/$key/"] = "I would like to pay now for $key $plu  ($value $currency)";
					
								}
								echo player_options($options);
				
				
				
        break;
        case 'rent_confirmed':
								$contract_length = $_REQUEST['v2'];
								$days = array();
								$days[1] = number_format($price * 1,0);
								$days[7] = number_format($price * 6.9,0);
								$days[10] = number_format($price * 9.7,0);
								$days[14] = number_format($price * 13.4,0);
								$days[30] = number_format($price * 29.3,0);
								if ($contract_length == 1 || $contract_length == 7 || $contract_length == 10 || $contract_length == 14 || $contract_length == 30)
								{
									// $destination should look like:
									// $destination = array('table' => $table,'assoc_id' => $account_id,'account_name' => $account_name);
								
									// $source should look like:
									// $source = array('assoc_id' => $user_id,'username' => $username);
									$source = array('assoc_id' => $user_id, 'username' => $user_info[$user_id]['username']);
									$destination = array('table' => 'government_money','assoc_id' => $account_id,'account_name' => government_setting('treasury_name'));
									$amount = $days[$contract_length];
									$result = transfer_money_user2computer($source,$destination,$amount,$currency);
									if ($result)
									{ 
											$serfdom->land_add($user_id,$land_type,'rent',$contract_length,$amount,government_setting('trees_on_land_min'),government_setting('trees_on_land_max'),$risk_of_fire);
											echo '<BR><BR><b>Signed. Sealed. Delivered.</b> The you can now manage the land from your '.ahref('/serfdom/ov/','overview').'.'; 
									} else { 
											echo '<BR><BR>Sorry there was a problem because '. $GLOBALS['error_message']; 
									}
		
								}
								else
								{
								 // looks like player has attempted to cheat by editing the url cause the numbers don't match
								}
								$options = array();
								foreach ($days as $key => $value)
								{
								 //
									if ($key <> 1){ $plu = 'days'; } else { $plu = 'day'; }
									$options["/serfdom/rent_confirmed/$land_type/$key/"] = "I would like to pay now for $key $plu  ($value $currency)";
					
								}
								$options['/serfdom/rent_choose/'] = 'Naa, I\'m not interested. Let me see other bits of land.';
								echo player_options($options);
								break;
								
								
			case 'ov':
								// need to display all entries from user_land
								// -> these must be links to the administration for this piece of land
								// -> -> the next page is the big one where u can choose lots of options (colonization type)
								/*
								output should be something like:
								
								you have access to the following estates:
								
								mountanious land from government <- link
								
								random land you stumbled upon in the Alps <- link
								
								*/
								 
								echo 'You have access to: <br>';
		
								$sql = "SELECT * FROM `user_land` WHERE `assoc_id`='$user_id'";
								$users_land = $db->fetch_real_array($sql);
								
								$options = array();
								//display_var($users_land);
								foreach ($users_land as $key => $value)
								{
											$land_options = str_split($value["land_type"]);
		
											$land_x = $land_options[0];
											$land_y = $land_options[1];
											$land_id = $value['id'];
											$options["/serfdom/farm/$land_id/"] = $land[$land_y]['ov'] .' '. $land[$land_x]['ov'] .'  ('.$value['land_type'].')';											
									//printf ("<BR>%s  %s (%s)\n", $land[$land_y]['ov'], $land[$land_x]['ov'], $value['land_type']);
								}
								
								$options['/serfdom/'] = '<b>Take me back to the main page</b>';
								echo player_options($options);
							break;
			
# the main farming screen
			case 'farm':
						$land_id = $_REQUEST['v1'];
						$action = @$_REQUEST['v2'];
						$v3 = @$_REQUEST['v3'];

						$users_land = $serfdom->load_land($user_id, $land_id);
						
						switch ($action){
						
# PLOUGH
								case 'pl': 
								$plough = $serfdom->land_plough($user_id,$land_id,$users_land['trees'],government_setting('effort_plough'));
									if ($plough)
									{
										echo "<pre>Land Plowed! <br>-- <br>+ $plough trees to stock</pre>";
									}
									else
									{
										echo '<pre>Couldn\'t plow land!</pre>';
									}
								break;
							
# FERTILIZE
								case 'ftz': 
									echo '<pre>Land(not) Fertilized!</pre>';
								break;

# MANURE (collection)
								case 'manure': 
								$collect = $serfdom->collect_manure($land_id,$users_land['amount_of_animals'],1);
									if ($collect)
									{
										echo "<pre>Manure Collected! <br>-- <br>+ $collect bits of manure</pre>";
									}
									else
									{
										echo '<pre>Couldn\'t collect any manure!</pre>';
									}
								break;

###########							
# PLANT SEEDS
###########
								case 'plant':
									$serfdom->land_plant($land_id,$v3,$farm_item_info[$v3]['seed_max']);
									echo '<pre>'.$v3.' seeds planted!</pre>';
								break;
								
###########							
# HARVEST 
###########
								case 'harvest':
								$effort_required = 0;
									if ($serfdom->harvest($user_id,$land_id,$users_land,$effort_required))
									{
										echo '<pre>Land Harvested!</pre>';
									}
									else
									{
										echo '<pre>You\'re harvest yielded nothing!</pre>';
									}
								break;
								
								
 # NOTHING/DEFAULT								
							default:
							// nothing to be done
						}
						
						
###########			#######				
# Land administration 
####################

						
						if ($users_land['id'] > 0)
						{
							$land_options = str_split($users_land["land_type"]);
							$land_x = $land_options[0];
							$land_y = $land_options[1];
							$land_desc = $land[$land_y]['ov'] .' '. $land[$land_x]['ov'];
							
							echo "Welcome to your $land_desc.<br>";
							
					# CROPS		
							$planted_on_land = $users_land['planted_on_land'];
							$current_crop = $users_land['day_of_cycle'];
							if (strlen($planted_on_land) > 0)
							{
								if ($current_crop == 1) { 
								echo "<br>Crops growing on this land: <b>$planted_on_land</b> (since yesterday)";
								} elseif ($current_crop > 1) { 
								echo "<br>Crops growing on this land: <b>$planted_on_land</b> (for <b>$current_crop</b> days)";
								} elseif ($current_crop == 0) { 
								echo "<br>Crops growing on this land: <b>$planted_on_land</b> (<i>just planted</i>)";
								}
								$harvest_option = 'I want to '.ahref("/serfdom/farm/$land_id/harvest/"," harvest the $planted_on_land");
							}
							else
							{
								echo '<br>You have nothing growing on this land.';
								$harvest_option = '<i>Nothign to harvest</i>';
							}
							
							
					# ANIMALS	
							echo '<br>';
							$animal_on_land = $users_land['animal_on_land'];
							$amount_of_animals = $users_land['amount_of_animals'];
							if (strlen($animal_on_land) > 0)
							{
								echo "<br>Livestock roaming this land: $animal_on_land (<b>$amount_of_animals</b>)";
							}
							else
							{
								echo '<br>You currently have no livestock.';
							}
					
					# FIRE RISK
							$risk_of_fire = $users_land['risk_of_fire'];
							if ($risk_of_fire > 95)
							{
								echo '<br><br><font color=red><b>Fire Risk</b></font>: Due to the <b>huge</b> amounts of <font color=red><b>dry scrub</b></font> on your land, you should be aware that a fire would spread at a phenomenal pace! Read up more on farming at the library and you might find a way to help counter this problem.';
							}
							elseif ($risk_of_fire > 68)
							{
								echo '<br><br><font color=red><b>Fire Risk</b></font>: There is a fair amount of dry scrub on this land, you should consider ways to get rid of it.';
							}
	
						
							echo '<h2>Prepare</h2>';
							$options_prepare = array();
							$options_prepare["/serfdom/farm/$land_id/pl/"] = 'Plough';
							$options_prepare["/serfdom/farm/$land_id/ftz/"] = 'Fertilize';
							echo '<p>'. player_options($options_prepare) .'</p><br>';
							
							$seed_info = $db->fetch_real_array("SELECT * FROM `user_items` WHERE `section`='farm' AND `type`='seed' AND `assoc_id`='$user_id'");
							//display_var($seed_info);
							$options = '<option default>choose...';
							foreach ($seed_info as $key => $val)
							{
							//echo "<br>key: $key, val: $val";
								$alias = $val['item'];
								$seed_name = $val['item'];
								$quantity = $val['quantity'];
								if ($quantity > 0)
								{
									$options .= "<option value=\"/en/$uid/serfdom/farm/$land_id/plant/$alias/\">$seed_name ($quantity)";
								}
							}
							
							echo '<h2>Farming</h2>';
							echo '<p><ul><li>i want to plant <select ONCHANGE="location = this.options[this.selectedIndex].value;">'.$options.'</select>
									<li>i want <input type=text length=10> <select><option>animal</select> to roam here</ul></p><br>';
									
							echo '<h2>Manage</h2>';
							$manure_link = ahref("/serfdom/farm/$land_id/manure/",'collect manure');
							echo '<p><ul><li>'.$harvest_option.'
									<li>with <select><option>animal</select> i want to <select><option>do something</select>
									<li>i want to '.$manure_link.'
									<li>i want to use <select><option>GM spray</select> on my land</ul></p><br>';
							
						}
						else
						{
							echo 'Strangely enough there is nothing here... '; // seems like the user is attempting to cheat
							game_error(403);
						}
						
						$options = array();
						//display_var($users_land);
						$options['/serfdom/'] = 'Take me back to the main page';
						echo player_options($options);
				break;
        
    default:
								// the first farming screen... must ask player how much land he wants and for how long, also what type of land
								echo 'welcome to farming...';
								//echo '<li>' . ahref();
								$options = array(
												 '/serfdom/rent_choose/' => 'i want to rent land',
												 '/serfdom/ov/' => 'i want to overview my land',
											   // 'I want land with forest' => 'serfdom/rent/forrest',
											   // 'I want land with forest' => 'serfdom/rent/forrest'
												 );
								echo player_options($options);
							
    
}
//$user_info[$user_id] = $user->load_tables('user_settings,user_finances',$user_id);
//echo '<pre>';
//echo var_dump($user_info);
//echo '</pre>';


?>